package com.fancy.mobileschool.ui;


import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.IBinder;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.fancy.mobileschool.MyConfiguration;
import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.Attendance;
import com.fancy.mobileschool.notify.AttendNotify;
import com.fancy.mobileschool.ui.fragment.MessageFragment;
import com.fancy.mobileschool.util.GsonUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
public class MainActivity extends AppCompatActivity implements TabHost.OnTabChangeListener{

    private FragmentTabHost fragmentTabHost;

    private Map<String, View> tabViews = new LinkedHashMap<>();

    private Gson gson;

    private List<Attendance> attendances;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gson = GsonUtil.getGson();
        attendances = new ArrayList<>();

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
            //actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            //actionBar.setCustomView(R.layout.custom_action_bar);
        }
        fragmentTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        initTabs();
        if (MyConfiguration.type.equals("student"));
            //startService();
    }


    private void initTabs() {
        fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.contentLayout);
        fragmentTabHost.getTabWidget().setDividerDrawable(null);
        fragmentTabHost.setOnTabChangedListener(this);
        for (int i = 0 ; i < Tabs.length ; i ++){
            TabHost.TabSpec tabSpec = fragmentTabHost.newTabSpec(Tabs.tabID[i]);
            tabSpec.setIndicator(getTabView(i));
            fragmentTabHost.addTab(tabSpec, Tabs.fragments[i], null);
            fragmentTabHost.setTag(i);
        }
    }


    @Override
    public void onTabChanged(String s) {
        updateTabColor(s);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }

    private View getTabView(int index) {
        View tabView = LayoutInflater.from(this).inflate(R.layout.tab_view, null);
        TextView textView = (TextView) tabView.findViewById(R.id.tabText);
        ImageView tabImg = (ImageView) tabView.findViewById(R.id.tabImg);
        if (index == 0){
            textView.setTextColor(Color.BLUE);
            tabImg.setColorFilter(Color.BLUE);
        }
        textView.setText(Tabs.texts[index]);
        textView.setTextSize(10);
        tabImg.setImageResource(Tabs.imgRes[index]);

        if (!tabViews.containsValue(tabView))
            tabViews.put(Tabs.tabID[index], tabView);

        return tabView;
    }

    private void updateTabColor(String tabTag){
        for (Map.Entry<String, View> entry : tabViews.entrySet()) {
            View tabView = entry.getValue();
            TextView textView = (TextView) tabView.findViewById(R.id.tabText);
            ImageView tabImg = (ImageView) tabView.findViewById(R.id.tabImg);
            if (entry.getKey().equals(tabTag)) {
                textView.setTextColor(getResources().getColor(R.color.tab_color));
                tabImg.setColorFilter(getResources().getColor(R.color.tab_color));
            }
            else{
                textView.setTextColor(Color.BLACK);
                tabImg.setColorFilter(Color.BLACK);
            }
        }
    }


    public void startService() {
        Intent i = new Intent(this, AttendNotify.class);
        bindService(i, new ServiceConn(), Context.BIND_AUTO_CREATE);

    }

    class ServiceConn implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            AttendNotify attendNotify = ((AttendNotify.MsgBinder)service).getService();
            attendNotify.setCallback(new MsgListner());

            attendNotify.listenerNotify();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    class MsgListner implements AttendNotify.GetDataCallback {

        @Override
        public void getData(String data) {

            Attendance attendance = null;
            try{
                attendance = gson.fromJson(data, Attendance.class);
            }catch (Exception e) {

            }

            if (attendance != null) {
                attendances.remove(attendance);
                attendances.add(attendance);
            }

        }
    }

    public List<Attendance> getAttendances() {
        return attendances;
    }


}
